using UnityEngine;

[CreateAssetMenu]
public class GameTileContentFactory : GameObjectFactory {

    [SerializeField]
    GameTileContent destinationPrefab = default;

    [SerializeField]
    GameTileContent emptyPrefab = default;
    
    [SerializeField]
    GameTileContent wallPrefab = default;
    
    [SerializeField]
    GameTileContent spawnPointPrefab = default;
    
    [SerializeField]
    Tower[] towerPrefabs = default;
    
    public void Reclaim (GameTileContent content) {
        Debug.Assert(content.OriginFactory == this, "错误的工厂回收!");
        Destroy(content.gameObject);
    }
    
    GameTileContent Get (GameTileContent prefab) {
        GameTileContent instance = CreateGameObjectInstance(prefab);
        instance.OriginFactory = this;
        return instance;
    }
    
    public GameTileContent Get (GameTileContentType type) {
        switch (type) {
            case GameTileContentType.Destination: return Get(destinationPrefab);
            case GameTileContentType.Empty: return Get(emptyPrefab);
            case GameTileContentType.Wall: return Get(wallPrefab);
            case GameTileContentType.SpawnPoint: return Get(spawnPointPrefab);
        }
        Debug.Assert(false, "不支持的非塔式: " + type);
        return null;
    }
    
    public Tower Get (TowerType type) {
        Debug.Assert((int)type < towerPrefabs.Length, "架空塔式!");
        Tower prefab = towerPrefabs[(int)type];
        Debug.Assert(type == prefab.TowerType, "索引错误的 Tower 预制件!");
        return Get(prefab);
    }
    
    T Get<T> (T prefab) where T : GameTileContent {
        T instance = CreateGameObjectInstance(prefab);
        instance.OriginFactory = this;
        return instance;
    }
}